package com.gt.util.serialize;

import com.alibaba.fastjson.JSONObject;
import com.esotericsoftware.kryo.Kryo;
import com.gt.util.model.Account;

import java.util.HashMap;
import java.util.Map;

/**
 * @author WUREN
 * @desc 对象序列化/反序列化工具类
 * @create 2019-02-25 23:29
 **/
public class SerializeUtil {
    /*
     * 序列化
     * */
    public static <T> byte[] serizlize(T t) {
        return ProtostuffUtil.serializer(t);
    }
    /*
     * 反序列化
     * */
    public static <T> T deserialize(byte[] bytes, Class<T> clazz) {
        return ProtostuffUtil.deserializer(bytes, clazz);
    }

    public static void main(String[] str) {
        Map map = new HashMap();
        map.put("key","hello world");
        Account account = new Account();
        account.setId(123);
        account.setUserName("username");
        account.setMap(map);
        long t1 = System.currentTimeMillis();
        byte[] bytes = FstUtil.serializer(account);
        long t2 = System.currentTimeMillis();

        Account res = FstUtil.deserializer(bytes, Account.class);
        long t3 = System.currentTimeMillis();

        System.out.println(bytes.length);
        System.out.println("FST序列化时长："+(t2-t1));
        System.out.println("FST反序列化时长："+(t3-t2));
        System.out.println(JSONObject.toJSONString(res));


        long t11 = System.currentTimeMillis();
        byte[] bytes2 = ProtostuffUtil.serializer(account);
        long t12 = System.currentTimeMillis();

        Account res2 = ProtostuffUtil.deserializer(bytes2, Account.class);
        long t13 = System.currentTimeMillis();

        System.out.println(bytes2.length);
        System.out.println("Protostuff序列化时长："+(t12-t11));
        System.out.println("Protostuff反序列化时长："+(t13-t12));
        System.out.println(JSONObject.toJSONString(res2));
    }
}

